package com.mjk.manager.task.type.sqlhttp;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import com.mjk.common.core.jdbc.service.parse.type.BeeParseType;
import com.mjk.common.tools.gson.GsonUtils;
import com.mjk.common.tools.scheduled.DynamicScheduledModel;
import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.task.dto.SqlTaskEntity;
import com.mjk.manager.task.type.ITaskInvoker;
import com.mjk.manager.task.type.sqlhttp.http.HttpSyncStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class SqlHttpTaskInvoker implements ITaskInvoker {
	@Autowired
	HttpSyncStrategy httpSync;
	@Override
	public void taskInvoker(DynamicScheduledModel model) {
		SqlTaskEntity entity = GsonUtils.json2Bean(model.getExt().toString(), SqlTaskEntity.class);
		Map<String,Object> params = GsonUtils.json2Map(model.getTaskParams());
		if(CollectionUtils.isEmpty(params)) {
			params = new HashMap<>();
		}
		Map<String,Object> sqlResult = BeeParseType.getParsing(BeeParseType.SQL).parsing(Objects.requireNonNull(entity).getSql(), params);
		//把sql结果带入下面流程
		for(HttpSyncStrategy.HttpSyncContent content : entity.getContent()) {
			if(ObjectUtils.isNotEmpty(model.getTaskParams())) { // 任务参数
				sqlResult.put("taskParams",model.getTaskParams());
			}
			content.setRequestParams(sqlResult);
			
			httpSync.pushContent(content);
		}
	}

}
